Thread: [Rookie] Defining Arrays

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    6

    [Rookie] Defining Arrays

    Hey everyone!

    I'm new to these forums and fairly new to the C language. I'm three weeks into my first coding class at school and things are starting to pick up intensity. So, on to my question!

    I'd like to prompt the user for a single 5-8 digit password and analyze it by redefining it as an array. I figured that an array would be easier to work with since I can analyze each digit specifically. A colleague of mine threw around the idea of using a remainder-loop sort of process to pick apart the code in a FIFO stack style.

    Any help is appreciated!

    EDIT: The passcode is entirely numerical, by the way

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It's common to use arrays to hold data that is sequential in the order it is given or needs to be processed. You can work with a char array as if it were any kind of stack you want, just be using the indices to analyze the char you want, in the order you want.

    You can work with numbers either as int's, floats, doubles, or simply as digits (chars).

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    I have a general idea of how an array works and how to target a specific element, but I'm trying to do something like this (not the actual syntax):

    Code:
    scanf("%8d", &passcode);
    
    // change (int passcode) into an int array or char array
    
    if (passarray[0] = 0)
    {
         printf("\npasscode must not start with '0'");
    }
    It's just changing the integer into an array for analysis that I'm struggling with.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    You're supposed to struggle!

    You need to have a firm grasp on using a loop with an index variable to reference each sequential element of the array.
    here if you need help.

    Then, play around with div and mod and see if you can get individual digits out of the number. Note that there are at least 2 approaches to this, one of which will give you the reverse of the original (well, depending on how you handle it).
    Hint: You will need to modify the original number throughout the process.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    scanf("%8d", &passcode);
    
    // change (int passcode) into an int array or char array
    
    if (passarray[0] = 0)
    {
         printf("\npasscode must not start with '0'");
    }
    1) You can't access a single digit of an integer like an element of an array.
    2) = is the assignment operator, == the comparison operator

    What kind of analysis do you want to do?

    Generally I would regard a password as a string instead of as a number. This would make it easy to read it into a char array without needing to mess around with divisions and remainders.

    Bye, Andreas

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    I know there were syntax errors, it was a very rough description of what I want to do.

    Now that you mention string, I feel like an idiot though :P So how can I transform a string into an array? Or can a string be treated similarly to an array? Every time I've used strings, it's been for simple things, ie: "What is your name?" type of questions. Not to mention that I haven't fully studied pointers yet.

    And the analysis I want to do is several points of inspection. The password must be 5-8 digits (done), must not start with a zero (will handle with array), last three digits must yield a prime number (will handle with array), and last five digits must form a palindrome (also handled with array comparisons).

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Strings are char arrays:
    Code:
    char myPasswordString[10];
    
    printf("Enter your password: ");
    fflush(stdout); //print the enter prompt, & leave the cursor where it is
    
    scanf("%s",myPasswordString);   //note no & here
    printf("%s \n",myPasswordString);

  8. #8
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Posts
    88
    Are you not allowed to use fgets()? If so I would avoid scanf() and read from STDIN with the former, then do some error checking on it to make sure it conforms to your assigment requirements.

  9. #9
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    Quote Originally Posted by hex_dump View Post
    Are you not allowed to use fgets()? If so I would avoid scanf() and read from STDIN with the former, then do some error checking on it to make sure it conforms to your assigment requirements.
    All of our assignments so far have been much simpler: just if...else and while or for loops. That being said, I'm trying not to get into too much too fast.

    Quick question, how can I compare a string to an integer without the compiler giving me fits? I need to set -1 as a sentinel value to quit out of the password-testing loop.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Why not compare the char '-1' with the sentinel value of a char '-1'? Chars can be a positive number up to 127, and a negative numbers down to -128.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Adak
    Why not compare the char '-1' with the sentinel value of a char '-1'? Chars can be a positive number up to 127, and a negative numbers down to -128.
    Heh, char might be unsigned, though in that case it would still work, unless you actually need the value of (unsigned char)-1.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Feb 2013
    Posts
    2
    Igrad, the solution depends on whether or not you need to be able to read each individual digit as it entered, or once they press enter, analyze each character on the line they submitted. You can store them both in the same way, but how you get them and process them depends on that distinction.

  13. #13
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    Quote Originally Posted by Darphant View Post
    Igrad, the solution depends on whether or not you need to be able to read each individual digit as it entered, or once they press enter, analyze each character on the line they submitted. You can store them both in the same way, but how you get them and process them depends on that distinction.
    After the user hits enter, I'd like to be able to analyze each digit of the entered number individually.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, the idea of reading it as a string into an array of char has already been suggested. Do you understand how to work with that idea?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    Roughly, yes. I probably just need to fool around with char and string concepts until I have a firm grasp on it. I'm just bumfuzzled as to how to initialize all these new char values without making my code 20 lines longer. Is there a cleaner, more efficient way to do it? (my current code's important features are below):

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    
    {
    
        char passcode[ 8 ] = {0};
        char negOne = -1;
        char zero = 0;
        char maxlim = 99999999;
        char minlim = 10000;
        int i = 0;
    
        int ProgQuit= 0;
    
        printf("\n\nWelcome, Agent.\nThis program will analyze the password you enter to see if it meets agency standard. Your password must meet the following qualifications.\n\n\t1. must be all numbers\n\t2. must not start with a zero\n\t3. must end in a three-digit prime number\n\t4. last 5 digits must be a palindrome\n\t5. must be 5 to 8 digits long\n\nYou may enter -1 when you are finished");
    
        do
        {
            /*Reset values to 0 for loop*/
            i = 0;
    
            /*Start passcode analyzing loop*/
            printf("\n\nPlease enter a passcode: ");
            scanf("%s", passcode);
    
            /*Set sentinel value to -1*/
            if(passcode == negOne) 
            {
                printf("\n\nExiting...\n\n");
                ProgQuit = -1;
            }
    
    // Other "else if" statements
    
        } while(ProgQuit != -1);
    
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rookie looking for help!
    By Newklear in forum C Programming
    Replies: 28
    Last Post: 03-11-2011, 05:15 AM
  2. Arrays: defining a new class in c++
    By jeremy-hulse in forum C++ Programming
    Replies: 7
    Last Post: 04-09-2010, 08:41 AM
  3. Defining char arrays as string literals
    By Programmer_P in forum C++ Programming
    Replies: 23
    Last Post: 06-06-2009, 07:18 AM
  4. Replies: 9
    Last Post: 01-26-2008, 03:12 AM
  5. Help a rookie
    By elrookie in forum C Programming
    Replies: 14
    Last Post: 06-14-2007, 10:28 AM

Tags for this Thread